<?php


namespace app\common\service\qrcode;


class GenerateCode
{
    // 生成普通二维码
    public function normalCode($text, $save = false)
    {
        // 生成二维码
        require root_path()."/vendor/phpqrcode/phpqrcode.php";
        $qRcode = new \QRcode();
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 4;
        $outfile = false;
        if ($save) {
            $path = '/qrcode/' . date('Ymd', time()) . '/';
            $saveName = md5(time() . uniqid()).'jpg';
            if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $path)) {
                mkdir($_SERVER['DOCUMENT_ROOT'] . $path, 0755, true);
            }
            $outfile = $_SERVER['DOCUMENT_ROOT']. $path .$saveName;
        }
        // 生成的文件名
        $qRcode->png($text, $outfile, $level, $size);
        if ($save) {
            return $path . $saveName;
        }
        $img =ob_get_contents();
        ob_end_clean();
        return 'data:png;base64,' . chunk_split(base64_encode($img));//转base64
    }

}